package com.ruoyi.client.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.client.domain.entity.BlogTags;
import com.ruoyi.client.domain.entity.BlogTagsCategory;
import com.ruoyi.client.service.IBlogCategoryService;
import com.ruoyi.client.service.IBlogTagsCategoryService;
import com.ruoyi.client.service.IBlogTagsService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.common.web.core.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("client/user/tag")
public class BlogTagController extends BaseController {
    @Autowired
    IBlogTagsService tagsService;
    @Autowired
    IBlogTagsCategoryService tagsCategoryService;

    @GetMapping("list")
    public TableDataInfo<BlogTags> list(PageQuery pageQuery){
        TableDataInfo<BlogTags> result = tagsService.selectBlogTagsList(pageQuery);
        return result;
    }
    @PostMapping
    public R add(@RequestBody BlogTags blogTags){
        if (tagsService.exists(Wrappers.<BlogTags>lambdaQuery().eq(BlogTags::getName,blogTags.getName()))) {
            throw new ServiceException("该标签已存在");
        }
        BlogTagsCategory one = tagsCategoryService.getOne(Wrappers.<BlogTagsCategory>lambdaQuery()
            .eq(BlogTagsCategory::getTagType, blogTags.getTagType())
        );
        //设置分类id
        blogTags.setCategoryId(one.getId());
        boolean save = tagsService.save(blogTags);
        return toAjax(save);
    }
    @GetMapping("/{id}")
    public R<BlogTags> getInfor(@PathVariable Long id){
        BlogTags blogTags = tagsService.getById(id);
        return R.ok(blogTags);
    }
    @PutMapping
    public R update(@RequestBody BlogTags blogTags){
        boolean b = tagsService.updateById(blogTags);
        return toAjax(b);
    }
    @DeleteMapping("/{ids}")
    public R delete(@PathVariable Long[] ids){
        boolean b = tagsService.removeBatchByIds(Arrays.asList(ids));
        return toAjax(b);
    }
    @GetMapping("/hot")
    public R<List<BlogTags>> getHotList(){
        List<BlogTags> list = tagsService.getBlogTagsByHot();
        return R.ok(list);
    }
}
